﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace WindowsFormProject.Depo
{
    public class UrunIslemler
    {


        public class UrunTut
        {

            public string      UrunId                { get; set; }
            public string   UrunADi                  { get; set; }
            public int      KatID                    { get; set; }
            public string KatAdi                     { get; set; }  
            public decimal  UrunStokAdet             { get; set; }
	        public double  UrunAlisFiyati            { get; set; }
            public double  UrunSatisFiyati           { get; set; }
            public decimal  UrunKDV                  { get; set; }
            public decimal  UrunMaxIskontoOrani      { get; set; }
	        public decimal  UrunIskontoOrani         { get; set; }
            public decimal  UrunSiparisICinMinStok   { get; set; }
            public bool     UrunSatistaMi            { get; set; } 
            public string   UrunSatisGirisTarihi     { get; set; }
            public string   UrunSatisCikisTarihi     { get; set; }
            public string   UrunSatisCikisSebebi     { get; set; }
            public int      UrunMuhasebeHesapNo      { get; set; }
            public string   UrunBirimi               { get; set; }
            public string   UrunParaCinsi            { get; set; } 
            public bool     UrunKurSabitMi           { get; set; }
            public double UrunKuru                   { get; set; } 

             public override string ToString()
            {
                return UrunId + "" + UrunADi + "" + KatID +  KatAdi  + "" + UrunStokAdet + "" + UrunAlisFiyati + "" + UrunSatisFiyati + "" + UrunKDV + "" + UrunMaxIskontoOrani + "" + UrunIskontoOrani + "" + UrunSiparisICinMinStok + "" + UrunSatistaMi + "" + UrunSatisGirisTarihi + "" + UrunSatisCikisTarihi + "" + UrunSatisCikisSebebi + "" + UrunMuhasebeHesapNo + "" + UrunBirimi + "" + UrunParaCinsi + "" + UrunKurSabitMi + "" + UrunKuru;
            
            }
        }
        public class UrundetayIslemler : IEnumerable
        {
            DataBaseProvider MyDataBase = new DataBaseProvider();
            List<UrunTut> Urundetaylarilist = new List<UrunTut>();
           
            public DataTable Urunler(string UrunId)
            {
                DataTable ReturnValue;
                MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, UrunId);
                ReturnValue = MyDataBase.ExecuteDataTable("	Select MU.*, KatagoriAdi from MErkezUrunler AS MU Left Outer JOIN Kategoriler AS K ON K.KatID= MU.KatID  where UrunId=@UrunId", CommandType.Text);
                MyDataBase.ClearParameters();
                return ReturnValue;


            }
            public DataTable TumUrunler()
            {
                DataTable ReturnValue;

                ReturnValue = MyDataBase.ExecuteDataTable("	Select MU.UrunId,MU.UrunADi,MU.UrunStokAdet from MErkezUrunler AS MU", CommandType.Text, "MErkezUrunler");
                MyDataBase.ClearParameters();
                return ReturnValue;


            }
            
           
            public List<UrunTut> UrunIdyeGore(string recID)
            {
                List<UrunTut> SecilenUrun = new List<UrunTut>();

                foreach (UrunTut item in Urundetaylarilist)
                {
                    if (item.UrunId == recID)
                    {
                        SecilenUrun.Add(item);
                    }
                }

                return SecilenUrun;
            }

            public void UrunEkle(UrunTut U)
            {
                Urundetaylarilist.Add(U);
            }
            public void UrunTutClear(UrunTut U)
            {
                Urundetaylarilist.Clear();
               
            }

            public int Get_RecordUrunCount(string UrunId)
            {
                int ReturnValue = 0;

                MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, UrunId);
                ReturnValue = Convert.ToInt32(MyDataBase.ExecuteScalar("Select UrunId from MErkezUrunler where UrunId = @UrunId", CommandType.Text));
                MyDataBase.ClearParameters();
                return ReturnValue;
            }
            public int Get_DeleteRecordUrunCount(string UrunId)
            {
                int ReturnValue = 0;

                MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, UrunId);
                ReturnValue = Convert.ToInt32(MyDataBase.ExecuteScalar("SELECT COUNT(*) from dbo.MErkezUrunler  MURUN Where	EXISTS ((Select UrunId  From dbo.AlisDetaylari AD where MURUN.UrunId=AD.UrunId OR  EXISTS (Select UrunId  From dbo.SatisDetaylari SD where MURUN.UrunId=SD.UrunId) OR  EXISTS (Select UrunId  From dbo.SubeUrunler SURUN where MURUN.UrunId=SURUN.UrunId)	OR  EXISTS (Select UrunId  From dbo.StokHaraketleri SH where SH.UrunId=MURUN.UrunId))) and		 MURUN.UrunId  =   @UrunId ", CommandType.Text));
                MyDataBase.ClearParameters();
                return ReturnValue;
            }


            public int Urundelete(string UrunId)
            {
                int ReturnValue = 0;
                DataBaseProvider MyDatabase = new DataBaseProvider();
                MyDatabase.AddInParameter("@UrunId", SqlDbType.NVarChar, UrunId);
                ReturnValue = Convert.ToInt32(MyDatabase.ExecuteNonQuery("Delete  MErkezUrunler where UrunId = @UrunId", CommandType.Text));
                MyDataBase.ClearParameters();
                return ReturnValue;

            }


            public int UrunVeriTabaninaKayit(UrunTut U)
            {

                int ReturnValue = 0;
                int ToplamEtkilenenKayitSayisi = 0;
                foreach (UrunTut item in Urundetaylarilist)
                {
                    try
                    {
                        MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, U.UrunId);                        
                        MyDataBase.AddInParameter("@UrunADi", SqlDbType.NVarChar, U.UrunADi);
                        MyDataBase.AddInParameter("@KatID", SqlDbType.Int,U.KatID);
                        MyDataBase.AddInParameter("@UrunStokAdet", SqlDbType.Decimal, U.UrunStokAdet);
                        MyDataBase.AddInParameter("@UrunAlisFiyati", SqlDbType.Money, U.UrunAlisFiyati);
                        MyDataBase.AddInParameter("@UrunSatisFiyati", SqlDbType.Money, U.UrunSatisFiyati);
                        MyDataBase.AddInParameter("@UrunKDV", SqlDbType.Decimal, U.UrunKDV);
                        MyDataBase.AddInParameter("@UrunMaxIskontoOrani", SqlDbType.Decimal, U.UrunMaxIskontoOrani);
                        MyDataBase.AddInParameter("@UrunIskontoOrani", SqlDbType.Decimal,U.UrunIskontoOrani);
                        MyDataBase.AddInParameter("@UrunSiparisICinMinStok", SqlDbType.Decimal,U.UrunSiparisICinMinStok);
                        MyDataBase.AddInParameter("@UrunSatistaMi", SqlDbType.Bit, U.UrunSatistaMi);
                        MyDataBase.AddInParameter("@UrunSatisGirisTarihi", SqlDbType.DateTime, U.UrunSatisGirisTarihi);
                        MyDataBase.AddInParameter("@UrunSatisCikisTarihi", SqlDbType.DateTime, U.UrunSatisCikisTarihi);
                        MyDataBase.AddInParameter("@UrunSatisCikisSebebi", SqlDbType.NVarChar, U.UrunSatisCikisSebebi);
                        MyDataBase.AddInParameter("@UrunMuhasebeHesapNo", SqlDbType.Int, U.UrunMuhasebeHesapNo);
                        MyDataBase.AddInParameter("@UrunBirimi", SqlDbType.NVarChar, U.UrunBirimi);
                        MyDataBase.AddInParameter("@UrunParaCinsi", SqlDbType.NVarChar, U.UrunParaCinsi);
                        MyDataBase.AddInParameter("@UrunKurSabitMi", SqlDbType.Bit, U.UrunKurSabitMi);
                        MyDataBase.AddInParameter("@UrunKuru", SqlDbType.Money, U.UrunKuru);
                        MyDataBase.ExecuteNonQuery("Insert Into MErkezUrunler (UrunId,UrunADi,KatID,UrunStokAdet,UrunAlisFiyati,UrunSatisFiyati,UrunKDV,UrunMaxIskontoOrani,UrunIskontoOrani,UrunSiparisICinMinStok,UrunSatistaMi,UrunSatisGirisTarihi,UrunSatisCikisTarihi,UrunSatisCikisSebebi,UrunMuhasebeHesapNo,UrunBirimi,UrunParaCinsi,UrunKurSabitMi,UrunKuru) values (@UrunId,@UrunADi,@KatID,@UrunStokAdet,@UrunAlisFiyati,@UrunSatisFiyati,@UrunKDV,@UrunMaxIskontoOrani,@UrunIskontoOrani,@UrunSiparisICinMinStok,@UrunSatistaMi,@UrunSatisGirisTarihi,@UrunSatisCikisTarihi,@UrunSatisCikisSebebi,@UrunMuhasebeHesapNo,@UrunBirimi,@UrunParaCinsi,@UrunKurSabitMi,@UrunKuru)", CommandType.Text);
                    }
                    catch
                    {
                    }
                    MyDataBase.ClearParameters();
                    return ReturnValue;
                }
                return ToplamEtkilenenKayitSayisi;
            }


            public int UrunKayitGuncelle(UrunTut U)
            {
                int ReturnValue = 0;
                foreach (UrunTut item in Urundetaylarilist)
                {
                    try
                    {
                        MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, U.UrunId);
                        MyDataBase.AddInParameter("@UrunADi", SqlDbType.NVarChar, U.UrunADi);
                        MyDataBase.AddInParameter("@KatID", SqlDbType.Int, U.KatID);
                        MyDataBase.AddInParameter("@UrunStokAdet", SqlDbType.Decimal, U.UrunStokAdet);
                        MyDataBase.AddInParameter("@UrunAlisFiyati", SqlDbType.Money, U.UrunAlisFiyati);
                        MyDataBase.AddInParameter("@UrunSatisFiyati", SqlDbType.Money, U.UrunSatisFiyati);
                        MyDataBase.AddInParameter("@UrunKDV", SqlDbType.Decimal, U.UrunKDV);
                        MyDataBase.AddInParameter("@UrunMaxIskontoOrani", SqlDbType.Decimal, U.UrunMaxIskontoOrani);
                        MyDataBase.AddInParameter("@UrunIskontoOrani", SqlDbType.Decimal, U.UrunIskontoOrani);
                        MyDataBase.AddInParameter("@UrunSiparisICinMinStok", SqlDbType.Decimal, U.UrunSiparisICinMinStok);
                        MyDataBase.AddInParameter("@UrunSatistaMi", SqlDbType.Bit, U.UrunSatistaMi);
                        MyDataBase.AddInParameter("@UrunSatisGirisTarihi", SqlDbType.DateTime, U.UrunSatisGirisTarihi);
                        MyDataBase.AddInParameter("@UrunSatisCikisTarihi", SqlDbType.DateTime, U.UrunSatisCikisTarihi);
                        MyDataBase.AddInParameter("@UrunSatisCikisSebebi", SqlDbType.NVarChar, U.UrunSatisCikisSebebi);
                        MyDataBase.AddInParameter("@UrunMuhasebeHesapNo", SqlDbType.Int, U.UrunMuhasebeHesapNo);
                        MyDataBase.AddInParameter("@UrunBirimi", SqlDbType.NVarChar, U.UrunBirimi);
                        MyDataBase.AddInParameter("@UrunParaCinsi", SqlDbType.NVarChar, U.UrunParaCinsi);
                        MyDataBase.AddInParameter("@UrunKurSabitMi", SqlDbType.Bit, U.UrunKurSabitMi);
                        MyDataBase.AddInParameter("@UrunKuru", SqlDbType.Money, U.UrunKuru);
                        MyDataBase.ExecuteNonQuery("Update MErkezUrunler SET UrunADi =@UrunADi,KatID=@KatID,UrunStokAdet=@UrunStokAdet,UrunAlisFiyati=@UrunAlisFiyati,UrunSatisFiyati=@UrunSatisFiyati,UrunKDV=@UrunKDV,UrunMaxIskontoOrani=@UrunMaxIskontoOrani,UrunIskontoOrani=@UrunIskontoOrani,UrunSiparisICinMinStok=@UrunSiparisICinMinStok, UrunSatistaMi=@UrunSatistaMi,UrunSatisGirisTarihi=@UrunSatisGirisTarihi,UrunSatisCikisTarihi=@UrunSatisCikisTarihi,UrunSatisCikisSebebi=@UrunSatisCikisSebebi,UrunMuhasebeHesapNo=@UrunMuhasebeHesapNo, UrunBirimi=@UrunBirimi,UrunParaCinsi=@UrunParaCinsi,UrunKurSabitMi=@UrunKurSabitMi,UrunKuru=@UrunKuru                      where UrunId=@UrunId", CommandType.Text);



                    }
                    catch
                    {

                    }
                 
                }
                MyDataBase.ClearParameters();
                return ReturnValue;
            }
            public IEnumerator GetEnumerator()
            {
                return new Gezdirici(Urundetaylarilist);

            }

            public class Gezdirici : IEnumerator
            {
                List<UrunTut> GelenListe;
                int ToplamEleman;
                int GecerliElemanNo;
                public Gezdirici(List<UrunTut> Liste)
                {
                    GelenListe = Liste;
                    ToplamEleman = Liste.Count;
                    GecerliElemanNo = -1;
                }
                public object Current
                {
                    get
                    {
                        return GelenListe[GecerliElemanNo];
                    }
                }

                public bool MoveNext()
                {

                    GecerliElemanNo++;
                    if (GecerliElemanNo < ToplamEleman)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

                public void Reset()
                {

                    GecerliElemanNo = -1;
                }

            }

        }

    }
}
